//
//  MinStack.h
//  leetcode
//
//  Created by hellobike07499 on 2019/8/30.
//  Copyright © 2019 hellobike07499. All rights reserved.
//

#ifndef MinStack_h
#define MinStack_h
#include <iostream>
#include <stack>
#include <vector>

using namespace std;
class MinStack {
public:
    static void test(){
        MinStack stack;
        stack.push(-2);
        stack.push(-1);
        stack.push(-3);
        
        stack.pop();
    }
public:
    /** initialize your data structure here. */
    MinStack() {
        
    }
    
    void push(int x) {
        if(mStack.size() == 0 || x < min){
            min = x;
        }
        
        mStack.push_back(x);
    }
    
    void pop() {
        if(mStack.rbegin() != mStack.rend()){
            int value = *(mStack.rbegin());
            
            mStack.erase(mStack.begin() + (mStack.size() - 1));
            if(value <= min){
                if(mStack.size() > 0) min = mStack[0];
                else min = 0;
                //find min
                vector<int>::iterator item = mStack.begin() ;
                while (item != mStack.end()) {
                    if(*item < min){
                        min = *item;
                    }
                    item++;
                }
            }
        }
    }
    
    int top() {
        return *(mStack.rbegin());
    }
    
    int getMin() {
        return min;
    }
private:
    vector<int> mStack;
    int min;
};


#endif /* MinStack_h */
